package com.youtui.manager.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.youtui.common.constant.ResultMessage;
import com.youtui.common.util.DateUtils;
import com.youtui.common.util.ResultUtil;
import com.youtui.manager.entity.WithdrawalEntity;
import com.youtui.manager.service.IWithdrawalService;

@Controller
@RequestMapping("/withdrawal")
public class WithdrawalController extends BaseController {

	@Autowired
	IWithdrawalService withdrawalService;

	@RequestMapping("/pagelist.do")
	@ResponseBody
	public ResultMessage findPageList(String mobile, String bankNo, Date applyDate,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "20") Integer rows) {

		PageInfo<WithdrawalEntity> pageinfo = withdrawalService.findWithdrawalPageList(mobile, bankNo, applyDate, page,
				rows);
		return ResultUtil.result(ResultUtil.code_success, "开发者提现列表", pageinfo);
	}

	@RequestMapping("/checkWithdrawal.do")
	@ResponseBody
	public ResultMessage checkWithdrawal(Long id, Integer auditType, String auditInfo) {

		if (id == null || auditType == null) {
			return ResultUtil.result(ResultUtil.code_fail, "请求参数错误");
		}
		ResultMessage result = withdrawalService.checkWithdrwal(id, auditType, auditInfo);
		return result;

	}

	@RequestMapping("/confirmWithdrawal.do")
	@ResponseBody
	public ResultMessage confirmWithdrawal(Long id, Integer auditType, String auditInfo) {

		if (id == null || auditType == null) {
			return ResultUtil.result(ResultUtil.code_fail, "请求参数错误");
		}
		ResultMessage result = withdrawalService.confirmWithdrawal(id, auditType, auditInfo);
		return result;

	}

	@RequestMapping("/export.do")
	@ResponseBody
	public void export(String mobile, String bankNo, Date applyDate) throws Exception {
		List<WithdrawalEntity> reportList = withdrawalService.findWithdrawalList(null, mobile, bankNo, applyDate);

		for (int i = 0; i < reportList.size(); i++) {
			reportList.get(i).setS_account_type(getAccountType(reportList.get(i).getAccountType()));
			reportList.get(i).setStatusStr(getStatusStr(reportList.get(i).getStatus()));
		}
		String fileName = "开发者提现报表" + DateUtils.getTimestamp();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("mobile", mobile);
		dataMap.put("bankNo", bankNo);
		dataMap.put("applyDate", applyDate);
		excelUtil.excelData(request, response, fileName, "withdrawal_excel", reportList, dataMap);
	}

	private String getAccountType(int type) {
		if (type == 2) {
			return "企业";
		}
		return "个人";
	}

	private String getStatusStr(int type) {
		switch (type) {
		case 0:
			return "已撤销";
		case 1:
			return "申请";
		case 2:
			return "已通过";
		case 3:
			return "已确认";
		default:
			break;
		}
		return "申请";

	}

}
